package com.example.myapplication.tack_13;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.R;
import com.example.myapplication.tack_13.adapter.CardsAdapter;
import com.example.myapplication.tack_13.data.CardData;
import com.example.myapplication.utils.Common;
import com.example.myapplication.utils.OkHttpTool;
import com.example.myapplication.utils.StateData;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PatientActivity extends AppCompatActivity {
    private ImageView exit;
    private RecyclerView list;
    private TextView cilck;
    private JSONObject jsonObject;
    private ImageView add;
    private  String jsonSex;
    private List<CardData.RowsDTO> cardList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tack_13_activity_patient);
        Intent intent = getIntent();
        String stringExtra = intent.getStringExtra(Common.COMMON);
        init();
        exit_();
        cilck_();
        add_();
        getArrage();
    }
    private void add_() {
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(PatientActivity.this);
                View view = getLayoutInflater().inflate(R.layout.tack_13_inflate,null);
                increase(view,builder);
            }
        });
    }
    private void cilck_() {
        cilck.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(PatientActivity.this);
                View view = getLayoutInflater().inflate(R.layout.tack_13_inflate,null);
                increase(view,builder);


            }
        });
    }

    private void increase(View view, AlertDialog.Builder builder) {
        EditText address = view.findViewById(R.id.address);
        EditText birthday = view.findViewById(R.id.birthday);
        EditText cardId = view.findViewById(R.id.cardId);
        EditText name = view.findViewById(R.id.name);
        RadioGroup radio_group = view.findViewById(R.id.radio_group);
        EditText tel = view.findViewById(R.id.tel);

        builder.setView(view);

        radio_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if(R.id.radio1 == group.getCheckedRadioButtonId()) {
                    jsonSex = "1";
                }else if(group.getCheckedRadioButtonId() ==  R.id.radio2){
                    jsonSex = "0";
                }
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder. setCancelable(false).setPositiveButton("确定", new DialogInterface.OnClickListener() {
            private String dateStr;
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Date date = null;
                String address1 = address.getText().toString();
                String birthday1 = birthday.getText().toString();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    date = sdf.parse(birthday1);
                    dateStr = sdf.format(date);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String cardId1 = cardId.getText().toString();
                String name1 = name.getText().toString();
                String tel1 = tel.getText().toString();
                try {

                    jsonObject.put("address",address1);
                    jsonObject.put("birthday",dateStr);
                    jsonObject.put("cardId",cardId1);
                    jsonObject.put("name",name1);
                    jsonObject.put("sex",jsonSex);
                    jsonObject.put("tel",tel1);
                    if(("".equals(jsonSex) || TextUtils.isEmpty(jsonSex) ) && ("".equals(address1) || TextUtils.isEmpty(address1))
                            && ("".equals(birthday1) || TextUtils.isEmpty(birthday1)) && ("".equals(cardId1) || TextUtils.isEmpty(cardId1))
                            && ("".equals(name1) || TextUtils.isEmpty(name1)) && ("".equals(tel1) || TextUtils.isEmpty(tel1))){

                            Toast.makeText(PatientActivity.this,"请输入信息",Toast.LENGTH_SHORT).show();
                            return;
                    }else {
                        String json = jsonObject.toString();
                        submit(json);
                        dialog.dismiss();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });

        builder.show();

    }


    private void submit(String json) {
        String url = "prod-api/api/hospital/patient";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, StateData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                StateData stateData = (StateData)obj;
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(stateData.getCode() == 200){
                            Toast.makeText(PatientActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                            getArrage();
                        }else if(stateData.getCode() != 200){
                            Toast.makeText(PatientActivity.this,stateData.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }
                });

            }

            @Override
            public void onFailure(String msg) {

            }
        },json);
    }

    private void getArrage() {
        String url = "prod-api/api/hospital/patient/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.POST, CardData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                CardData cardData = (CardData)obj;
                if(cardData.getCode() == 200){
                    cardList = cardData.getRows();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            cardAdapter();
                        }
                    });
                }


            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getArrage();
    }

    private void cardAdapter() {
        CardsAdapter cardsAdapter = new CardsAdapter(cardList, PatientActivity.this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(PatientActivity.this);
        list.setAdapter(cardsAdapter);
        list.setLayoutManager(linearLayoutManager);
    }

    private void exit_() {
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PatientActivity.this.finish();;
            }
        });
    }

    private void init() {
        exit = findViewById(R.id.exit);
        list = findViewById(R.id.list);
        cilck = findViewById(R.id.cilck);
        jsonObject = new JSONObject();
        add = findViewById(R.id.add);
    }
}